// Copyright (c) 2007 Alistair Jones
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import java.util.Arrays;
import java.util.List;

public class CodegenUtil {
    public static final int TARGET_LINE_WIDTH = 120;

    public static String capitalise(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    public static String splitOntoMultipleLines(String longLine) {
        StringBuffer outputBuffer = new StringBuffer();
        String[] tokens = longLine.split(", ");
        StringBuffer lineBuffer = new StringBuffer(tokens[0]);
        for (String token : Arrays.asList(tokens).subList(1, tokens.length)) {
            if (lineBuffer.length() + ", ".length() + token.length() + ",".length() > TARGET_LINE_WIDTH) {
                lineBuffer.append(",");
                outputBuffer.append(lineBuffer.toString()).append("\n");
                lineBuffer = new StringBuffer("        ");
            } else {
                lineBuffer.append(", ");
            }
            lineBuffer.append(token);
        }
        outputBuffer.append(lineBuffer.toString());
        return outputBuffer.toString();
    }

    static String commaSeparate(List<String> tokens) {
        String list = "";
        for (int i = 0; i < tokens.size(); i++) {
            if (i > 0) {
                list += ", ";
            }
            list += tokens.get(i);
        }
        return list;
    }

    public static String removeSpecialCharacters(String name) {
        return name.replaceAll("[^a-zA-Z]", "");
    }
}
